<template>
  <el-drawer
    title="保存画像模板"
    size="60%"
    :visible.sync="drawerVisible"
    direction="rtl"
    :close-on-press-escape="false"
    :wrapperClosable="false"
    :destroy-on-close="true"
    ref="tagDrawer"
    @closed="clearDrawer"
    custom-class="tag-drawer"
    v-drawerResize
  >
    <div class="drawer-wrap">
      <div class="drawer-content tag-scrollbar">
        <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
          <el-form-item prop="cover" label="覆盖旧模板">
            <el-radio-group v-model="editForm.isCover">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item prop="portraitId" label="覆盖模板名称" v-if="editForm.isCover">
            <el-select v-model="editForm.portraitId" filterable @change="changeTemp">
              <el-option
                v-for="item in tempList"
                :key="item.portraitId"
                :value="item.portraitId"
                :label="item.portraitName"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="portraitName" label="模板名称">
            <el-input placeholder="请输入模板名称" v-model="editForm.portraitName"></el-input>
          </el-form-item>
          <el-form-item prop="portraitDescription" label="模板说明">
            <el-input
              type="textarea"
              placeholder="请输入模板说明"
              v-model="editForm.portraitDescription"
              :rows="3"
              :maxlength="200"
              :show-word-limit="true"
              resize="none"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="drawer-btns">
      <el-button @click="closeDrawer">取消</el-button>
      <el-button type="primary" size="medium" @click="save">确定</el-button>
    </div>
  </el-drawer>
</template>

<script>
export default {
  data() {
    return {
      drawerVisible: false,
      tempList: [],
      editForm: {
        isCover: 0,
        portraitId: '',
        portraitName: '',
        portraitDescription: '',
        tagConfig: ''
      },
      editFormRules: {
        portraitId: [{ required: true, message: '覆盖模板不能为空', trigger: 'change' }],
        portraitName: [{ required: true, message: '模板名称不能为空', trigger: 'blur' }],
        portraitDescription: [{ required: true, message: '模板说明不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    showDrawer(json) {
      this.editForm.tagConfig = json
      this.getTempList()
      this.drawerVisible = true
    },
    getTempList() {
      this.$ajax({
        url: this.$ajax.setUrl('tagUserPortrait/getUserPortraitInfoAll'),
        method: 'get',
        params: this.$ajax.setParams()
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.tempList = data.data
        } else {
          this.$message.error(data.data || data.msg)
        }
      })
    },
    changeTemp(portraitId) {
      let data = this.tempList.find(item => item.portraitId === portraitId)
      this.editForm.portraitName = data.portraitName
      this.editForm.portraitDescription = data.portraitDescription
    },
    save() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.$ajax({
            url: this.$ajax.setUrl('tagUserPortrait/saveUserPortraitInfo'),
            method: 'post',
            data: this.$ajax.setData({
              isCover: this.editForm.isCover,
              portraitName: this.editForm.portraitName,
              portraitDescription: this.editForm.portraitDescription,
              tagConfig: this.editForm.tagConfig,
              portraitId: this.editForm.isCover ? this.editForm.portraitId : ''
            })
          }).then(({ data }) => {
            if (data && data.code === '0') {
              this.$message.success(data.msg)
              this.closeDrawer()
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        }
      })
    },
    closeDrawer() {
      this.$refs.tagDrawer.closeDrawer()
    },
    clearDrawer() {
      Object.assign(this.$data, this.$options.data.call(this))
    }
  }
}
</script>

<style lang="scss"></style>
